package com.example.wldcmzy.sutdy004;

import android.app.DatePickerDialog;
import android.content.DialogInterface;
import android.support.v7.app.AlertDialog;
import android.support.v7.app.AppCompatActivity;
import android.os.Bundle;
import android.view.View;
import android.view.ViewGroup;
import android.widget.Button;
import android.widget.CompoundButton;
import android.widget.DatePicker;
import android.widget.ImageButton;
import android.widget.ImageView;
import android.widget.LinearLayout;
import android.widget.Switch;
import android.widget.TextView;
import android.widget.Toast;
import android.widget.ToggleButton;

public class MainActivity extends AppCompatActivity {

    private ImageView img;
    private TextView txt, mstat, ostat, datet, timet;
    private ImageButton lf, rt;
    private  int[] images = {R.drawable.yy, R.drawable.yy2, R.drawable.yy3};
    private String[] texts = {"黑吼", "阴影", "消逝"};
    private int nowPage = 0, totalPage = images.length;
    private Button mstab, ostab, dateb, timeb;
    private String msta;
    private boolean[] ostaChoosed = {false, false, false};
    private Switch sch;
    private ToggleButton tb;
    private LinearLayout l1, l2;


    @Override
    protected void onCreate(Bundle savedInstanceState) {
        super.onCreate(savedInstanceState);
        setContentView(R.layout.activity_main);

        img = (ImageView) findViewById(R.id.img);
        txt = (TextView) findViewById(R.id.txt);
        lf = (ImageButton) findViewById(R.id.lf);
        rt = (ImageButton) findViewById(R.id.rt);
        mstab = (Button) findViewById(R.id.mstab);
        mstat = (TextView) findViewById(R.id.mstat);
        ostab = (Button) findViewById(R.id.ostab);
        ostat = (TextView) findViewById(R.id.ostat);
        sch = (Switch) findViewById(R.id.switchb);
        tb = (ToggleButton)findViewById(R.id.sdate);
        l1 = (LinearLayout) findViewById(R.id.Linear1);
        l2 = (LinearLayout) findViewById(R.id.Linear2);
        dateb = (Button) findViewById(R.id.dateb);
        timeb = (Button) findViewById(R.id.timeb);
        datet = (TextView) findViewById(R.id.datet);
        timet = (TextView) findViewById(R.id.timet);

        lf.setOnClickListener(onClickListener0);
        rt.setOnClickListener(onClickListener0);
        mstab.setOnClickListener(onClickListener0);
        ostab.setOnClickListener(onClickListener0);
        sch.setOnCheckedChangeListener(onCompoundButtonOnCheckedChange0);
        tb.setOnCheckedChangeListener(onCompoundButtonOnCheckedChange0);
/*        dateb.setOnClickListener(onClickListener1);
        timeb.setOnClickListener(onClickListener1);*/

    }

    private CompoundButton.OnCheckedChangeListener onCompoundButtonOnCheckedChange0 =
            new CompoundButton.OnCheckedChangeListener(){
                public void onCheckedChanged(CompoundButton compoundButton, boolean checked){
                    int id = compoundButton.getId();
                    switch (id){
                        case R.id.switchb:
                            if(checked){
                                ViewGroup.LayoutParams params = img.getLayoutParams();
                                params.width = (int) getResources().getDimension(R.dimen.bigSize);
                                params.height = (int) getResources().getDimension(R.dimen.bigSize);
                                img.setLayoutParams(params);
                            }else{
                                ViewGroup.LayoutParams params = img.getLayoutParams();
                                params.width = (int) getResources().getDimension(R.dimen.middleSize);
                                params.height = (int) getResources().getDimension(R.dimen.middleSize);
                                img.setLayoutParams(params);
                            }
                            break;
                        case R.id.sdate:
                            if(checked){
                                l1.setVisibility(View.VISIBLE);
                                l2.setVisibility(View.VISIBLE);
                            }else{
                                l1.setVisibility(View.GONE); // 不可见，没了
                                l2.setVisibility(View.INVISIBLE); // 不可见，占用原来的空间
                            }
                            break;
                    }

                }
            };

/*    private View.OnClickListener onClickListener1 = new View.OnClickListener(){
        public void onClick(View v){
            switch (v.getId()){
                case R.id.dateb:
                    DatePickerDialog datePicker = new DatePickerDialog(MainActivity.this,
                            new DatePickerDialog.OnDateSetListener(){

                            } )
            }
        }
    }*/

    private View.OnClickListener onClickListener0 = new View.OnClickListener(){
        public void onClick(View v){
            switch(v.getId()){
                case R.id.lf:
                    if(nowPage == 0){
                        //Toast.makeText(MainActivity.this, "第一页,自动循环到最后一页", Toast.LENGTH_SHORT).show();
                        AlertDialog.Builder builder = new AlertDialog.Builder(MainActivity.this);
                        builder.setMessage("第一页,自动循环到最后一页")
                            .setTitle("提示")
                            .setPositiveButton("确认",
                                new DialogInterface.OnClickListener() {
                                    @Override
                                    public void onClick(DialogInterface dialog, int which) {
                                        dialog.dismiss();
                                    }
                                })
                            .setNegativeButton("给我回去",
                                new DialogInterface.OnClickListener() {
                                    @Override
                                    public void onClick(DialogInterface dialog, int which) {
                                        nowPage = 0;
                                        img.setImageResource(images[nowPage]);
                                        txt.setText(texts[nowPage]);
                                        dialog.dismiss();
                                    }
                                })
                            .setNeutralButton("查看详情",
                                new DialogInterface.OnClickListener() {
                                    @Override
                                    public void onClick(DialogInterface dialog, int which) {
                                        Toast.makeText(MainActivity.this, "你无法查看", Toast.LENGTH_SHORT).show();
                                        dialog.dismiss();
                                    }
                                })
                            .show();
                    }
                    nowPage = (nowPage + totalPage - 1) % totalPage;
                    img.setImageResource(images[nowPage]);
                    txt.setText(texts[nowPage]);
                    break;
                case R.id.rt:
                    if(nowPage == totalPage - 1){
                        //Toast.makeText(MainActivity.this, "最后一页,自动循环到第一页", Toast.LENGTH_SHORT).show();
                        AlertDialog.Builder builder = new AlertDialog.Builder(MainActivity.this);
                        builder.setMessage("最后一页,自动循环到第一页")
                            .setTitle("提示")
                            .setPositiveButton("确认",
                                new DialogInterface.OnClickListener() {
                                    @Override
                                    public void onClick(DialogInterface dialog, int which) {
                                        dialog.dismiss();
                                    }
                                })
                            .setNegativeButton("给我回去",
                                new DialogInterface.OnClickListener() {
                                    @Override
                                    public void onClick(DialogInterface dialog, int which) {
                                        nowPage = totalPage - 1;
                                        img.setImageResource(images[nowPage]);
                                        txt.setText(texts[nowPage]);
                                        dialog.dismiss();
                                    }
                                })
                            .setNeutralButton("查看详情",
                                new DialogInterface.OnClickListener() {
                                    @Override
                                    public void onClick(DialogInterface dialog, int which) {
                                        Toast.makeText(MainActivity.this, "你无法查看", Toast.LENGTH_SHORT).show();
                                        dialog.dismiss();
                                    }
                                })
                            .show();
                    }
                    nowPage = (nowPage + 1) % totalPage;
                    img.setImageResource(images[nowPage]);
                    txt.setText(texts[nowPage]);
                    break;
                case R.id.mstab:
                    msta = "";
                    AlertDialog.Builder builder = new AlertDialog.Builder(MainActivity.this);
                    builder.setTitle("选择我的状态")
                            .setIcon(R.drawable.gl)
                            .setSingleChoiceItems(texts, //选项是数组
                                    -1, // -1代表没有选中项 0代表选中第一个
                                    new DialogInterface.OnClickListener() {
                                        @Override
                                        public void onClick(DialogInterface dialog, int which){
                                            msta = texts[which].toString();
                                        }
                                    })
                            .setPositiveButton("确认",
                                    new DialogInterface.OnClickListener() {
                                        @Override
                                        public void onClick(DialogInterface dialog, int which) {
                                            if(msta.equals("")){
                                                mstat.setText("仍然尚未选择");
                                            }else{
                                                mstat.setText(msta);
                                                dialog.dismiss();
                                            }
                                        }
                                    })
                            .setNegativeButton("取消",
                                    new DialogInterface.OnClickListener() {
                                        @Override
                                        public void onClick(DialogInterface dialog, int which) {
                                            mstat.setText("上次放弃选择");
                                            dialog.dismiss();
                                        }
                                    })
                            .create()
                            .show();
                    break;
                case R.id.ostab:
                    AlertDialog.Builder builder2 = new AlertDialog.Builder(MainActivity.this);
                    builder2.setTitle("选择其他人的状态")
                            .setIcon(R.drawable.bzz)
                            .setMultiChoiceItems(texts, //选项是数组
                                    ostaChoosed, //bool数组
                                    new DialogInterface.OnMultiChoiceClickListener() {
                                        @Override
                                        public void onClick(DialogInterface dialog, int which, boolean isChecked) {
                                            ostaChoosed[which] = isChecked;
                                        }
                                    })
                            .setPositiveButton("确定",
                                    new DialogInterface.OnClickListener() {
                                        @Override
                                        public void onClick(DialogInterface dialog, int which) {
                                            String choosed = "";
                                            for(int i=0; i<totalPage; i ++){
                                                if(ostaChoosed[i] == true){
                                                    if(choosed.equals("") == false) choosed += " ";
                                                    choosed += texts[i];
                                                }
                                            }
                                            if(choosed.equals("")) choosed = "祝你能在丝之鸽遇到其他骑士";
                                            ostat.setText(choosed);
                                        }
                                    })
                            .create()
                            .show();
                    break;
                default:
                    break;
            }

        }
    };
}
